package com.starblink.android.common.adapter.cell

import android.view.ViewGroup
import com.starblink.android.basic.adapter.BaseVH
import com.starblink.android.basic.ext.commContext
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.util.post.PostListDataHolder
import com.starblink.basic.route.RoutePage
import com.starblink.rocketreserver.fragment.PostF
import view.post.PostCardView

class DiscoverPostCardCell(
    parent: ViewGroup,
    val postList: ArrayList<PostF>,
    val scene: Int = 0,
    val keyWord: String = "",
    val searchPage: Int = 1,
    val searchType: Int = -1,
    val spmPageValue: Int = 0,
    val isLastOne: () -> Unit
) : BaseVH<PostF>(PostCardView(parent.context)) {

    private val cardView = itemView as PostCardView

    override fun onBind(post: PostF, position: Int) {
        itemView.tag = post
        cardView.bindPost(post, position, true,spmPageValue) {
            // 计算position对应post，在postList中的对应索引
            var realPosition = -1
            var i = 0
            while (i < postList.size) {
                // 此处使用===比较引用是否相同，因此postList和adapter中的post数据必须同源
                if (post === postList[i]) {
                    realPosition = i
                    break
                }
                i++
            }
            if (realPosition != -1) {
                PostListDataHolder.getInstance().setList(postList)
                commContext().navigationTo(if (post.contentType == "1") RoutePage.Post.PAGE_PIC else RoutePage.PostVideo.PAGE_VIDEO) {
//                    withObject(RoutePage.Post.POST_LIST, postList)
                    withString(RoutePage.Post.POST_ID, post.id)
                    withString(RoutePage.Post.TRACE_ID, post.traceId)
                    withString(RoutePage.Search.ARG_KEY_WORD, keyWord)
                    withInt(RoutePage.Post.ARG_SCENE, scene)
                    withInt(RoutePage.Post.POST_SEARCH_TYPE, searchType)
                    withInt(RoutePage.Post.POST_PAGE, searchPage)
                    withInt("index", realPosition)
                }
            }
        }
        if (postList.size > 4 && position == postList.size - 6) {
            isLastOne()
        }
    }
}